package com.onlinesshop.mall.controller;

import com.onlinesshop.mall.pojo.Product;
import com.onlinesshop.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class ViewController {

    @Autowired
    private ProductService productService;
    /**
     * 项目默认跳转index页面
     * @param modelAndView
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView toIndex(ModelAndView modelAndView){
        //随机查询8条产品数据
        List<Product> product8 = productService.findRandom(8);
        //随机查询出15条衣服的数据
        findClothesProduct15(modelAndView);
        //随机查询出15条运动健康的数据
        findRunProduct15(modelAndView);
        //保存八条数据到product8
        modelAndView.addObject("product8",product8);

        modelAndView.setViewName("index");
        return modelAndView;
    }

    /**
     * 随机查询出15条衣服的数据
     * @param modelAndView
     */
    public void findClothesProduct15(ModelAndView modelAndView){
        //根据分类id随机查询衣服的15条产品数据
        List<Product> product15 = productService.findByCategoryIdRandom(8,15);

        List<Product> product4=new ArrayList<Product>();
        product4.add(product15.get(0));
        product4.add(product15.get(1));
        product4.add(product15.get(2));
        product4.add(product15.get(3));
        List<Product> product4s=new ArrayList<Product>();
        product4s.add(product15.get(4));
        product4s.add(product15.get(5));
        product4s.add(product15.get(6));
        product4s.add(product15.get(7));
        List<Product> product2=new ArrayList<Product>();
        product2.add(product15.get(8));
        product2.add(product15.get(9));
        List<Product> product2_left=new ArrayList<Product>();
        product2_left.add(product15.get(10));
        product2_left.add(product15.get(11));
        List<Product> product2_right=new ArrayList<Product>();
        product2_right.add(product15.get(12));
        product2_right.add(product15.get(13));
        Product product1=product15.get(14);

        //保存产品为衣服数据4条到product4
        modelAndView.addObject("product4",product4);
        //保存产品为衣服数据4条到product4
        modelAndView.addObject("product4s",product4s);
        //保存产品为衣服数据2条到product2
        modelAndView.addObject("product2",product2);
        //保存产品为衣服数据2条到product2_left 和product2_rignt
        modelAndView.addObject("product2_left",product2_left);
        modelAndView.addObject("product2_right",product2_right);
        modelAndView.addObject("product1",product1);

    }
    /**
     * 随机查询出15条运动健康的数据
     * @param modelAndView
     */
    public void findRunProduct15(ModelAndView modelAndView){
        //根据分类id随机查询运动健康的15条产品数据
        List<Product> product15 = productService.findByCategoryIdRandom(17,15);

        List<Product> RunProduct4=new ArrayList<Product>();
        RunProduct4.add(product15.get(0));
        RunProduct4.add(product15.get(1));
        RunProduct4.add(product15.get(2));
        RunProduct4.add(product15.get(3));
        List<Product> RunProduct4s=new ArrayList<Product>();
        RunProduct4s.add(product15.get(4));
        RunProduct4s.add(product15.get(5));
        RunProduct4s.add(product15.get(6));
        RunProduct4s.add(product15.get(7));
        List<Product> RunProduct2=new ArrayList<Product>();
        RunProduct2.add(product15.get(8));
        RunProduct2.add(product15.get(9));
        List<Product> RunProduct2_left=new ArrayList<Product>();
        RunProduct2_left.add(product15.get(10));
        RunProduct2_left.add(product15.get(11));
        List<Product> RunProduct2_right=new ArrayList<Product>();
        RunProduct2_right.add(product15.get(12));
        RunProduct2_right.add(product15.get(13));
        Product RunProduct1=product15.get(14);

        //保存产品为衣服数据4条到product4
        modelAndView.addObject("RunProduct4",RunProduct4);
        //保存产品为衣服数据4条到product4
        modelAndView.addObject("RunProduct4s",RunProduct4s);
        //保存产品为衣服数据2条到product2
        modelAndView.addObject("RunProduct2",RunProduct2);
        //保存产品为衣服数据2条到product2_left 和product2_rignt
        modelAndView.addObject("RunProduct2_left",RunProduct2_left);
        modelAndView.addObject("RunProduct2_right",RunProduct2_right);
        modelAndView.addObject("RunProduct1",RunProduct1);

    }



    /**
     * 跳转至用户登录界面
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin(){
        System.out.println("测试成功");
        return "login";
    }

    /**
     * 跳转至用户注册界面
     * @return
     */
    @RequestMapping("/toRegister")
    public String toRegister(){
        return "registered";
    }

    /**
     * 跳转至订单生成页面
     * @return
     */
    @RequestMapping("/toOrder")
    public String toOrder(){

        return "Orders";
    }
}
